﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Fantom.Builders;

namespace Fantom.Models
{
	internal class Slides : ObservableCollection<ISlide>, ISlides
	{
		#region events
		public event SelectionEventHandler OnSelectionChanged;
		public event SelectableEventHandler OnSelectAll;
		public event SelectableEventHandler LostSelectAll;
		#endregion

		#region properties

		public IApplication Application { get; set; }

		public IFantomObject Parent { get; set; }

		public IEnumerable<ISlide> SelectedSlides => from sld in this.Items where sld.IsSelected select sld;

		public IShapes Shapes { get; private set; } = new Shapes();

		public IEnumerable<ISelectable> Selection => throw new System.NotImplementedException();

		#endregion

		#region ctors

		#endregion

		#region methods

		public ISlide New()
		{
			var sld = SlideBuilder.Build();
			base.Add(sld);
			return sld;
		}

		public void Select(IEnumerable<int> idRange)
		{
			var newRange = new Collection<ISlide>();
			var lostRange = new Collection<ISlide>();

			for (int i = 0; i < Items.Count; i++)
			{
				var sld = Items[i] as Slide;
				if (idRange.Contains(i) && !sld.IsSelected)
				{
					sld.IsSelected = true;
					newRange.Add(sld);
				}
				else if (!idRange.Contains(i) && sld.IsSelected)
				{
					sld.IsSelected = false;
					lostRange.Add(sld);
				}
			}

			OnSelectionChanged?.Invoke(this, new SelectionEventArgs(SelectedSlides, lostRange, newRange));
		}

		public void Select(int id)
		{
			Select(new[] { id });
		}

		public void SelectAdditional(IEnumerable<int> idRange)
		{
			var newRange = new Collection<ISlide>();
			var lostRange = new Collection<ISlide>();

			for (int i = 0; i < Items.Count; i++)
			{
				var sld = Items[i] as Slide;
				if (idRange.Contains(i) && !sld.IsSelected)
				{
					sld.IsSelected = true;
					newRange.Add(sld);
				}
			}

			OnSelectionChanged?.Invoke(this, new SelectionEventArgs(SelectedSlides, lostRange, newRange));
		}

		public void SelectAdditional(int id)
		{
			SelectAdditional(new[] { id });
		}

		public void Delete(ISlide slide)
		{
			Remove(slide);
		}

		public void Delete(IEnumerable<ISlide> slideRange)
		{
			foreach (var item in slideRange)
			{
				Remove(item);
			}
		}

		public void Select(IEnumerable<ISelectable> objRange)
		{
			throw new System.NotImplementedException();
		}

		public void SelectAdditional(IEnumerable<ISelectable> objRange)
		{
			throw new System.NotImplementedException();
		}

		public void Select(ISelectable obj)
		{
			throw new System.NotImplementedException();
		}

		public void SelectAdditional(ISelectable iobjd)
		{
			throw new System.NotImplementedException();
		}

		public void UnSelect(ISelectable obj)
		{
			throw new System.NotImplementedException();
		}

		public void UnSelect(IEnumerable<ISelectable> objRange)
		{
			throw new System.NotImplementedException();
		}

		public void SelectAll()
		{
			throw new System.NotImplementedException();
		}

		public void UnSelectAll()
		{
			throw new System.NotImplementedException();
		}

		#endregion
	}
}
